<script setup lang="ts">
import { ref, computed } from "vue";
import { useRoute, useRouter } from "vue-router";
import {
  House,
  Document,
  ArrowLeft,
  ArrowRight,
  Folder,
  ChatDotRound,
} from "@element-plus/icons-vue";

const route = useRoute();
const router = useRouter();
const isCollapse = ref(false);

// 导航菜单数据
const menuItems = [
  {
    index: "/",
    icon: House,
    title: "首页",
  },
  {
    index: "/medical-ai",
    icon: ChatDotRound,
    title: "AI问诊",
  },
  {
    index: "order-management",
    icon: Folder,
    title: "订单管理",
    children: [
      {
        index: "/orders/refund",
        icon: Document,
        title: "退款申请",
      },
      {
        index: "/orders/medicine",
        icon: Document,
        title: "药品订单管理",
      },
      {
        index: "/orders/prescription",
        icon: Document,
        title: "处方订单管理",
      },
      {
        index: "/orders/consultation",
        icon: Document,
        title: "问诊单管理",
      },
      {
        index: "/orders/registration",
        icon: Document,
        title: "挂号单管理",
      },
    ],
  },
];

// 计算当前激活的菜单项
const activeIndex = computed(() => route.path);

// 处理菜单点击
const handleMenuClick = (path: string) => {
  router.push(path);
};
</script>

<template>
  <div id="app">
    <el-container class="app-container">
      <!-- 左侧导航 -->
      <el-aside :width="isCollapse ? '64px' : '200px'" class="app-aside">
        <div class="logo-container">
          <h2 v-if="!isCollapse">订单管理系统</h2>
          <h2 v-else>订单</h2>
        </div>

        <el-menu
          :default-active="activeIndex"
          :collapse="isCollapse"
          class="sidebar-menu"
          background-color="#304156"
          text-color="#bfcbd9"
          active-text-color="#409eff"
        >
          <template v-for="item in menuItems" :key="item.index">
            <!-- 有子菜单的项目 -->
            <el-sub-menu v-if="item.children" :index="item.index">
              <template #title>
                <el-icon><component :is="item.icon" /></el-icon>
                <span>{{ item.title }}</span>
              </template>
              <el-menu-item
                v-for="child in item.children"
                :key="child.index"
                :index="child.index"
                @click="handleMenuClick(child.index)"
              >
                <el-icon><component :is="child.icon" /></el-icon>
                <template #title>{{ child.title }}</template>
              </el-menu-item>
            </el-sub-menu>

            <!-- 没有子菜单的项目 -->
            <el-menu-item
              v-else
              :index="item.index"
              @click="handleMenuClick(item.index)"
            >
              <el-icon><component :is="item.icon" /></el-icon>
              <template #title>{{ item.title }}</template>
            </el-menu-item>
          </template>
        </el-menu>

        <!-- 折叠按钮 -->
        <div class="collapse-btn" @click="isCollapse = !isCollapse">
          <el-icon>
            <ArrowLeft v-if="!isCollapse" />
            <ArrowRight v-else />
          </el-icon>
        </div>
      </el-aside>

      <!-- 右侧主内容区 -->
      <el-container class="main-container">
        <!-- 顶部导航栏 -->
        <el-header class="app-header">
          <div class="header-content">
            <div class="breadcrumb">
              <el-breadcrumb separator="/">
                <el-breadcrumb-item :to="{ path: '/' }"
                  >首页</el-breadcrumb-item
                >
                <el-breadcrumb-item
                  v-if="
                    route.path.startsWith('/orders') && route.path !== '/orders'
                  "
                >
                  订单管理
                </el-breadcrumb-item>
                <el-breadcrumb-item v-if="route.path !== '/'">
                  {{ route.meta?.title || "页面" }}
                </el-breadcrumb-item>
              </el-breadcrumb>
            </div>

            <div class="header-actions">
              <el-tag type="info" size="small">{{
                route.meta?.title || "首页"
              }}</el-tag>
            </div>
          </div>
        </el-header>

        <!-- 主内容区 -->
        <el-main class="app-main">
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<style>
#app {
  height: 100vh;
}

.app-container {
  height: 100vh;
}

.app-aside {
  background: linear-gradient(180deg, #1e1e2d 0%, #2d3748 100%);
  transition: width 0.3s;
  position: relative;
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
}

.logo-container {
  height: 70px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: #fff;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
}

.logo-container h2 {
  margin: 0;
  font-size: 18px;
  color: #fff;
  font-weight: 600;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.sidebar-menu {
  border: none;
  height: calc(100vh - 120px);
  background-color: #1e1e2d !important;
}

/* 主菜单项样式 */
.sidebar-menu .el-menu-item {
  height: 56px;
  line-height: 56px;
  color: #a8b2d1 !important;
  font-weight: 500;
  font-size: 14px;
  border-radius: 8px;
  margin: 4px 8px;
  transition: all 0.3s ease;
}

.sidebar-menu .el-menu-item:hover {
  background-color: #2d3748 !important;
  color: #ffffff !important;
  transform: translateX(4px);
}

.sidebar-menu .el-menu-item.is-active {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
  color: #ffffff !important;
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}

/* 子菜单项样式 */
.sidebar-menu .el-sub-menu .el-menu-item {
  height: 48px;
  line-height: 48px;
  padding-left: 32px !important;
  color: #a8b2d1 !important;
  font-weight: 400;
  font-size: 13px;
  border-radius: 6px;
  margin: 2px 12px;
  transition: all 0.3s ease;
}

.sidebar-menu .el-sub-menu .el-menu-item:hover {
  background-color: #2d3748 !important;
  color: #ffffff !important;
  transform: translateX(4px);
}

.sidebar-menu .el-sub-menu .el-menu-item.is-active {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
  color: #ffffff !important;
  box-shadow: 0 3px 8px rgba(102, 126, 234, 0.3);
}

/* 父级菜单标题样式 */
.sidebar-menu .el-sub-menu__title {
  height: 56px;
  line-height: 56px;
  color: #a8b2d1 !important;
  font-weight: 500;
  font-size: 14px;
  border-radius: 8px;
  margin: 4px 8px;
  transition: all 0.3s ease;
}

.sidebar-menu .el-sub-menu__title:hover {
  background-color: #2d3748 !important;
  color: #ffffff !important;
}

/* 图标样式优化 */
.sidebar-menu .el-icon {
  font-size: 18px;
  margin-right: 12px;
  transition: all 0.3s ease;
}

.sidebar-menu .el-menu-item:hover .el-icon,
.sidebar-menu .el-sub-menu__title:hover .el-icon {
  color: #ffffff !important;
  transform: scale(1.1);
}

.sidebar-menu .el-menu-item.is-active .el-icon,
.sidebar-menu .el-sub-menu .el-menu-item.is-active .el-icon {
  color: #ffffff !important;
}

/* 子菜单容器样式 */
.sidebar-menu .el-sub-menu .el-menu {
  background-color: #1e1e2d !important;
  border: none;
  padding: 8px 0;
}

/* 折叠箭头样式 */
.sidebar-menu .el-sub-menu__icon-arrow {
  color: #a8b2d1 !important;
  transition: all 0.3s ease;
}

.sidebar-menu .el-sub-menu__title:hover .el-sub-menu__icon-arrow {
  color: #ffffff !important;
}

.collapse-btn {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  width: 36px;
  height: 36px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: #fff;
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
  border: 2px solid rgba(255, 255, 255, 0.1);
}

.collapse-btn:hover {
  background: linear-gradient(135deg, #5a67d8 0%, #6b46c1 100%);
  transform: translateX(-50%) scale(1.1);
  box-shadow: 0 6px 16px rgba(102, 126, 234, 0.6);
}

.main-container {
  background-color: #f0f2f5;
}

.app-header {
  background-color: #fff;
  border-bottom: 1px solid #e8e8e8;
  padding: 0;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 100%;
  padding: 0 20px;
}

.breadcrumb {
  flex: 1;
}

.header-actions {
  display: flex;
  gap: 10px;
}

.app-main {
  padding: 20px;
  background-color: #f0f2f5;
  min-height: calc(100vh - 60px);
}
</style>
